package com.handinfo.android.ui.window;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.core.net.GameProtocol;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.Role;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.Soul;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlTools;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWExternalDraw;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWGrid;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWInputBox;
import com.handinfo.android.uicontrols.controls.DWRadioButton;
import com.handinfo.android.uicontrols.controls.DWTabControl;
import com.handinfo.android.uicontrols.controls.DWTabPage;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

public class UISoulWindow implements IUIWindows{
	private DWFrame m_soul_frame;
	private Bitmap m_img_close = null;//叉子图片
	public Bitmap m_tgBitmapTab01 = null;//标签页图片01
	public Bitmap m_tgBitmapTab02 = null;//标签页图片02
	private Bitmap m_img_background = null;
	private Bitmap m_img_icon[] = null;
	private Bitmap m_img_name[] = null;
	private Bitmap img_img_title = null;
	private Bitmap m_img_bk;
	private DWTabControl tabControl;
	private DWTabPage tabPage[];
	private DWTextbox m_tbstr[][];
	private DWTextbox m_tbname[];
	
	private Bitmap img_button;
	private Bitmap img_anxia;
	
	private Bitmap m_img_qihun = null;
	private Bitmap m_img_qihuns = null;
	
	private String m_soulstr[]={"气魂","力魂","天魂","地魂","灵魂","精魂"};
	
	private static final String m_soulstr_name[][]={
			{
				"中府","云门","天府","侠白","尺泽","孔最","列缺","经渠","太渊","鱼际","少商"
			},
			{
				"天池","天泉","曲泽","郄门","间使","内关","大陵","劳宫","中冲","乌鱼","毛头"
			},
			{
				"极泉","青灵","少海","灵道","通里","阴郄","神门","少府","少冲","萧凤","至玉"
			},
			{
				"商阳","二间","三间","合谷","阳溪","偏历","温溜","下廉","上廉","曲池","迎香"
			},
			{
				"关冲","液门","中渚","阳池","外关","支沟","会宗","四渎","天井","消泺","臑会"
			},
			{
				"少泽","前谷","后溪","腕骨","阳谷","养老","支正","小海","肩贞","臑俞","天宗"
			},
	};
	private String m_soulstr_menu[] = {
			"生命值","攻击力","物理防御","法术防御","命中值","回避值"
	};
	private String m_strps[] ={
			"提升最大生命值",
			"提升攻击力",
			"提升物理防御",
			"提升法术防御",
			"提升命中值",
			"提升回避值",
	};
	private byte m_index = 0;
	
	private  static final float m_qihunxy[][][] = {
			{
				{
					130,271
				},
				{
					220,208
				},
				{
					277,115
				},
				{
					361,113
				},
				{
					361,191
				},
				{
					361,269
				},
				{
					482,270
				},
				{
					604,271
				},
				{
					497,208
				},
				{
					453,115
				},
				{
					363,32
				},
				
			},
			{
				{
					55,184
				},
				{
					136,116
				},
				{
					221,202
				},
				{
					276,117
				},
				{
					361,238
				},
				{
					497,202
				},
				{
					675,184
				},
				{
					587,116
				},
				{
					453,117
				},
				{
					361,136
				},
				{
					361,51
				},
				
			},
			{
				{
					179,88
				},
				{
					264,126
				},
				{
					361,164
				},
				{
					470,211
				},
				{
					592,262
				},
				{
					361,298
				},
				{
					130,262
				},
				{
					245,211
				},
				{
					462,126
				},
				{
					537,88
				},
				{
					361,51
				},
				
			},
			{
				{
					129,273
				},
				{
					178,90
				},
				{
					361,51
				},
				{
					536,87
				},
				{
					535,256
				},
				{
					361,298
				},
				{
					245,211
				},
				{
					314,112
				},
				{
					445,131
				},
				{
					420,221
				},
				{
					361,165
				},
				
			},
			{
				{
					68,290
				},
				{
					187,263
				},
				{
					257,183
				},
				{
					180,90
				},
				{
					313,112
				},
				{
					361,51
				},
				{
					417,112
				},
				{
					536,90
				},
				{
					460,183
				},
				{
					535,263
				},
				{
					646,290
				},
				
			},
			{
				{
					68,290
				},
				{
					187,263
				},
				{
					257,183
				},
				{
					313,112
				},
				{
					361,51
				},
				{
					479,53
				},
				{
					535,119
				},
				{
					460,183
				},
				{
					535,263
				},
				{
					646,290
				},
				{
					682,157
				},
				
			}
	};
	
	private int m_jieduan[];
	
	private float m_fen = 50;
	private float m_fens = 50;
	
	private float m_fenw = 0;
	private float m_fenh = 0;
	private DWGrid grid[][];
	private DWImageBox closeImgBox;
	
	public void initFrame(){
		m_soul_frame = new DWFrame(DWFrame.FRAME_TYPE_FULL);
		//标题
		DWTitle title = new DWTitle("", m_soul_frame);
		m_soul_frame.addControl(title);
		DWImageBox m_tit_ima = new DWImageBox(img_img_title);
		m_tit_ima.setNearAnchor(title, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER, 0, 0);
		m_soul_frame.addControl(m_tit_ima);
		
		//叉子
		closeImgBox=new DWImageBox(m_img_close);
		closeImgBox.addListener(new DWListener() {			
			public void OnDoubleClick(){ 
			}
			public void OnClick()
			{
				close((byte) 0);
			}
		});
		closeImgBox.setNearAnchor(title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_soul_frame.addControl(closeImgBox);
		//按钮
		int y=0;
		int w = 0;
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			y=25;
			w=40;
		}else{
			y=10;
			w=20;
		}
		
		tabControl = new DWTabControl(m_tgBitmapTab01, m_tgBitmapTab02, m_soul_frame.getShowWidth() - 20,
				m_soul_frame.getShowHeight() - w);
		tabControl.setNearAnchor(m_soul_frame, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, y);
//		tabControl.setSelectPage(m_index);
		tabControl.addListener(new DWListener() {
			
			public void OnDoubleClick() {
				// TODO Auto-generated method stub
			}
			public void OnClick() {
					if(m_index!=(byte) tabControl.getPageIndex()){
						m_index = (byte) tabControl.getPageIndex();
						DWGameManager.getInstance().getSendMessage().sendOpenSoulByIndex((byte)m_index);
						touchfalse(false);
					}
				//testAnnouncement();
			}
		});
		

		
		int size = m_soulstr.length;
		tabPage = new DWTabPage[size];
		for(int i = 0;i<size;i++){
			tabPage[i] = new DWTabPage();
			tabPage[i].setName(m_soulstr[i]);
		}
		
		if(m_soulList==null){
			return;
		}
		size = m_soulList.size();
		m_tbstr = new DWTextbox[size][m_qihunxy[0].length];
		m_tbname = new DWTextbox[size];
		grid = new DWGrid[size][];
		for(int t=0;t<size;t++){
			final int t_index = t;
			
			DWImageBox backgound = new DWImageBox(m_img_bk);
			backgound.setNearAnchor(tabPage[t], Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 0);
			tabPage[t].addControls(backgound);
			
			DWImageBox name =new DWImageBox(m_img_name[t]);
			name.setNearAnchor(backgound, Tools.LEFT_TOP, Tools.LEFT_TOP,40,15);
			tabPage[t].addControls(name);
			
			m_tbname[t] = new DWTextbox("<mark c=FF0000>"+m_strps[t]+":"+m_curValue+"</mark>");
			if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
				m_tbname[t].setFont(DWFont.getFont(32));
			}else{
				m_tbname[t].setFont(DWFont.getFont(22));
			}
			m_tbname[t].setNearAnchor(name, Tools.LEFT_TOP, Tools.LEFT_TOP,name.getShowWidth(),name.getShowHeight()-m_tbname[t].getShowHeight()>>1);
			tabPage[t].addControls(m_tbname[t]);
			
			
			
			//绘制线
			DWImageBox xian =new DWImageBox(backgound.getShowWidth(),backgound.getShowHeight());
			xian.setNearAnchor(backgound, Tools.LEFT_TOP, Tools.LEFT_TOP);
			xian.addExternalDraw(new DWExternalDraw() {
				
				public void externalDraw(DWGraphics g, int x, int y) {
					
					for(int i=0;i<m_qihunxy[t_index].length;i++){
						if(i!=m_qihunxy[t_index].length-1){
							float xs = x+m_qihunxy[t_index][i][0]*m_fenw+m_img_icon[0].getWidth()/2;
							float ys = y+m_qihunxy[t_index][i][1]*m_fenh+m_img_icon[0].getHeight()/2;
							float xs1 = x+m_qihunxy[t_index][i+1][0]*m_fenw+m_img_icon[0].getWidth()/2;
							float ys1 = y+m_qihunxy[t_index][i+1][1]*m_fenh+m_img_icon[0].getHeight()/2;
							
							g.drawLines(xs, ys, xs1, ys1,3,0xFF725e7f);
						}
					}
					
					for(int i=0;i<m_jieduan[t_index];i++){
						if(m_jieduan[t_index]>=m_qihunxy[t_index].length){
							m_jieduan[t_index] = m_qihunxy[t_index].length-1;
						}
						float xs = x+m_qihunxy[t_index][i][0]*m_fenw+m_img_icon[0].getWidth()/2;
						float ys = y+m_qihunxy[t_index][i][1]*m_fenh+m_img_icon[0].getHeight()/2;
						float xs1 = x+m_qihunxy[t_index][i+1][0]*m_fenw+m_img_icon[0].getWidth()/2;
						float ys1 = y+m_qihunxy[t_index][i+1][1]*m_fenh+m_img_icon[0].getHeight()/2;
						
						g.drawLines(xs, ys, xs1, ys1,5,0xFF74d5bf);
						
					}
				}
			});
			
			tabPage[t].addControls(xian);
			
			//绘制魂位
			final CopyOnWriteArrayList<Soul> list = getSoulListByIndex(t_index);
			if(list!=null){
				grid[t] = new DWGrid[list.size()];
				for(int i=0;i<list.size();i++){
					grid[t][i] = new DWGrid(0, 0, list.get(i).m_未激活.getWidth(), list.get(i).m_未激活.getHeight());
					grid[t][i].setNearAnchor(backgound, Tools.LEFT_TOP, Tools.LEFT_TOP,(int)(list.get(i).m_x*m_fenw), (int)(list.get(i).m_y*m_fenh));
					final Soul soul = list.get(i);
					grid[t][i].m_data = soul;
					grid[t][i].setDarg(false);
					grid[t][i].addListener(new DWListener() {
						public void OnDoubleClick() {
							// TODO Auto-generated method stub
						}
						public void OnClick() {
							// TODO Auto-generated method stub
							//drawmenu(t_index, is);
							if(soul.isAlive()){
								m_rb_ceng=1;
								m_rb_index=0;
								drawmenu(soul);
							}else{
								touchfalse(false);
								DWGameManager.getInstance().getSendMessage().sendSeeSoulByIndex(soul);
							}
							
						}
					});
					tabPage[t].addControls(grid[t][i]);
					
					m_tbstr[t][i] = new DWTextbox(list.get(i).m_name);
					if(list.get(i).m_state==Soul.状态_正在激活){
						m_tbstr[t][i].setText(list.get(i).m_name+list.get(i).m_num+"层");
					}
					m_tbstr[t][i].setNearAnchor(grid[t][i], Tools.LEFT_TOP, Tools.LEFT_TOP,(grid[t][i].getShowWidth()-m_tbstr[t][i].getShowWidth()>>1)-5,
							0-m_tbstr[t][i].getShowHeight()-3);
					tabPage[t].addControls(m_tbstr[t][i]);
				}
			}
				
				//绘制说明文字
					final DWImageBox img = new DWImageBox(backgound.getShowWidth(),backgound.getShowHeight());
					img.setNearAnchor(backgound, Tools.LEFT_TOP, Tools.LEFT_TOP);
					img.addExternalDraw(new DWExternalDraw() {
						
						public void externalDraw(DWGraphics g, int x, int y) {
							g.setColor(0xAA000000);
							g.fillRect(x+20, y+img.getShowHeight() - (g.getFontHeight()+4), img.getShowWidth()-40, g.getFontHeight()+4);
							g.drawString("修炼魂位层级直接提升各种属性", 0xFFFFC004, x+20+((int)((img.getShowWidth()-40)-DWFont.getDefaultFont().stringWidth("修炼魂位层级直接提升各种属性"))>>1),
									y+img.getShowHeight() - (g.getFontHeight()+2), 0);
						}
					});
					img.setTouch(false);
					tabPage[t].addControls(img);
		}
		
		for(int i = 0;i<m_soulstr.length;i++){
			tabControl.addTabPage(tabPage[i]);
		}
		m_soul_frame.addControl(tabControl);

	}

	private String[] m_money={"金钱","精魂"};
	public Bitmap m_cr_true = null; ///下面的单选被选中时
	public Bitmap m_cr_false = null;///下面的单选没被选中时
	private DWRadioButton m_rb;
	private int m_rb_index;
	private short m_rb_ceng=1;
	private Bitmap m_Lbutton;
	private Bitmap m_Rbutton;
	private DWTextbox tb4;

	

	public void drawmenu(final Soul soul){
		
	
		float xs = soul.m_x *m_fenw;
		//float ys = soul.m_y *m_fenh;
		final DWFrame soul_frame = new DWFrame();
		int h1;
		if( soul.m_state == Soul.状态_正在激活 ){
			if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
				h1=130;
			}else{
				h1=80;
			}
		}else{
			if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
				h1=350;
			}else{
				h1=250;
			}
		}
		if(xs<DWControlsManager.CONTROL_WIDTH / 2){
			soul_frame.setPoint(DWGameManager.Screen_Width / 2 + (DWGameManager.Screen_Width / 2 - (DWControlsManager.CONTROL_WIDTH / 2 - 30)) / 2, (DWGameManager.Screen_Height - (DWControlsManager.CONTROL_HEIGHT - h1)) / 2);
		}else{
			soul_frame.setPoint((DWGameManager.Screen_Width / 2 - (DWControlsManager.CONTROL_WIDTH / 2 - 30)) / 2, (DWGameManager.Screen_Height - (DWControlsManager.CONTROL_HEIGHT - h1)) / 2);
		}
		soul_frame.m_null_frame = false;
		soul_frame.setShowWideHigh(DWControlsManager.CONTROL_WIDTH / 2 - 30, DWControlsManager.CONTROL_HEIGHT - h1);
		soul_frame.setClickClose(false);
		//标题
		DWTitle title = new DWTitle(soul.m_name, soul_frame);
		soul_frame.addControl(title);
		//叉子
		DWImageBox closeImgBox=new DWImageBox(m_img_close);
		closeImgBox.addListener(new DWListener() {			
			public void OnDoubleClick()
			{
			}
			public void OnClick()
			{
				DWControlsManager.getInstance().removeControl(soul_frame);
			}
		});
		closeImgBox.setNearAnchor(title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		soul_frame.addControl(closeImgBox);
		//背景
		
		DWBackground backgound=new DWBackground(soul_frame.getShowWidth()-20, soul_frame.getShowHeight()-50);
		backgound.setNearAnchor(soul_frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 25);
		soul_frame.addControl(backgound);
		
		
		int h = 50;
		DWTextbox tb1 = new DWTextbox("<mark c=FFC004>"+soul.m_name+soul.m_num+"/"+soul.m_max+"层"+"</mark>"+DWControlTools.CHANGE_ROW +"升满可提升"+m_soulstr_menu[soul.m_page]+soul.m_value);
		if( soul.m_state != Soul.状态_正在激活 ){
			tb1.setText("<mark c=FFC004>"+soul.m_name+"</mark>"+DWControlTools.CHANGE_ROW +"升满可提升"+m_soulstr_menu[soul.m_page]+soul.m_value);
		}
		tb1.setNearAnchor(backgound, Tools.LEFT_TOP, Tools.LEFT_TOP,30,h);
		soul_frame.addControl(tb1);
		
		
		if( soul.m_state == Soul.状态_正在激活 ){
				
			DWTextbox tb3 = new DWTextbox("提升层数：");
			tb3.setNearAnchor(tb1, Tools.LEFT_TOP, Tools.LEFT_TOP,0,tb1.getShowHeight()+5);
			soul_frame.addControl(tb3);
			
			//修炼多少层
			final DWInputBox soullv = new DWInputBox(140, DWFont.getDefaultFont().getHeight()+4, soul_frame);
			soullv.setText(""+m_rb_ceng);
			soullv.setNearAnchor(tb3, Tools.LEFT_TOP, Tools.LEFT_TOP,60,tb3.getShowHeight()+10);
			soullv.addListener(new DWListener() {
				
				public void OnDoubleClick() {
					// TODO Auto-generated method stub
				}
				public void OnClick() {
					// TODO Auto-generated method stub
					if (UINpcShopWindow.isNumber(soullv.getText())) {
						int num = Integer.valueOf(soullv.getText());
						if (num < 0) {
							DWGameManager.getInstance().addSystemInfo(
									DWGameManager.SYSTEM_INFO_TYPE_B, "请输入正确的数字！");
							m_rb_ceng = (short) 1;
							soullv.setText(""+m_rb_ceng);
							m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
							m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
							tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
//							DWControlsManager.getInstance().removeControl(soul_frame);
//							drawmenu(soul);
						} else if (num == 0) {
							DWGameManager.getInstance().addSystemInfo(
									DWGameManager.SYSTEM_INFO_TYPE_B, "修炼层级不得为0！");
							m_rb_ceng = (short) 1;
							soullv.setText(""+m_rb_ceng);
							m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
							m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
							tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
//							DWControlsManager.getInstance().removeControl(soul_frame);
//							drawmenu(soul);
						} else if(num>(soul.m_max-soul.m_num)){
							DWGameManager.getInstance().addSystemInfo(
									DWGameManager.SYSTEM_INFO_TYPE_B, "您修炼的层级不得大于最高层级！");
							m_rb_ceng = (short) 1;
							soullv.setText(""+m_rb_ceng);
							m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
							m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
							tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
//							DWControlsManager.getInstance().removeControl(soul_frame);
//							drawmenu(soul);
						}else{
							m_rb_ceng = (short) num;
							soullv.setText(""+m_rb_ceng);
							m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
							m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
							tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
//							DWControlsManager.getInstance().removeControl(soul_frame);
//							drawmenu(soul);
						}
					}else{
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B, "请输入正确的数字！");
						m_rb_ceng = (short) 1;
						soullv.setText(""+m_rb_ceng);
						m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
						m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
						tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
//						DWControlsManager.getInstance().removeControl(soul_frame);
//						drawmenu(soul);	
					}
				}
			});
			soul_frame.addControl(soullv);
			
			//左箭头
			DWImageBox imgl=new DWImageBox(m_Lbutton);
			imgl.setNearAnchor(soullv, Tools.LEFT_TOP, Tools.LEFT_TOP,0-m_Lbutton.getWidth()-10,soullv.getShowHeight()-m_Lbutton.getHeight()>>1);
			imgl.addListener(new DWListener() {
				public void OnDoubleClick() {
					// TODO Auto-generated method stub
				}
				public void OnClick() {
					// TODO Auto-generated method stub
					if(m_rb_ceng>1){
						m_rb_ceng--;
						soullv.setText(""+m_rb_ceng);
						m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
						m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
						tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
//						DWControlsManager.getInstance().removeControl(soul_frame);
//						drawmenu(soul);
					}else{
						m_rb_ceng = (short) (soul.m_max-soul.m_num);
						soullv.setText(""+m_rb_ceng);
						m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
						m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
						tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
					}
				}
			});
			soul_frame.addControl(imgl);
			
			//右箭头
			DWImageBox imgr=new DWImageBox(m_Rbutton);
			imgr.setNearAnchor(soullv, Tools.LEFT_TOP, Tools.LEFT_TOP,soullv.getShowWidth()+10,soullv.getShowHeight()-m_Rbutton.getHeight()>>1);
			imgr.addListener(new DWListener() {
				public void OnDoubleClick() {
					// TODO Auto-generated method stub
				}
				public void OnClick() {
					// TODO Auto-generated method stub
					if(m_rb_ceng<soul.m_max-soul.m_num){
						m_rb_ceng++;
						soullv.setText(""+m_rb_ceng);
						m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
						m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
						tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
//						DWControlsManager.getInstance().removeControl(soul_frame);
//						drawmenu(soul);
					}else{
						m_rb_ceng=1;
						soullv.setText(""+m_rb_ceng);
						m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
						m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
						tb4.setText("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
					}
				}
			});
			soul_frame.addControl(imgr);
			
			m_money[0] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceByMoney))+"金";
			m_money[1] =""+(m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit))+"精魂";
			
			//总值
			 tb4 = new DWTextbox("需要："+DWControlTools.CHANGE_ROW +"         "+m_money[0]+DWControlTools.CHANGE_ROW+"         或"+m_money[1]);
			tb4.setNearAnchor(tb3, Tools.LEFT_TOP, Tools.LEFT_TOP,0,soullv.getShowHeight()+tb3.getShowHeight()+30);
			soul_frame.addControl(tb4);
			

			
			

			

			
			DWButton button1 = new DWButton("使用精魂", img_button);
			button1.setDownImage(img_anxia);
			button1.setNearAnchor(backgound, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 10, -10);
			button1.addListener(new DWListener() {
				public void OnDoubleClick() {
				}
				public void OnClick() {
					Role role = DWGameManager.getInstance().m_role;
					if(role.m_jinghun>=m_rb_ceng*Integer.valueOf(soul.m_priceBySpirit)){
						if( soul.m_state == Soul.状态_正在激活 ){
							DWGameManager.getInstance().getSendMessage().sendPromoteSoulLevel((byte)soul.m_page,soul.m_index,(byte)1,m_rb_ceng);
						}
						DWControlsManager.getInstance().removeControl(soul_frame);
					}else{
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B, "您的精魂不足！");
					}
				}
			});
			
			
			DWButton button2=new DWButton("使用金币", img_button);
			button2.setDownImage(img_anxia);
			button2.setNearAnchor(backgound, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -10, -10);
			button2.addListener(new DWListener() {
				public void OnDoubleClick() {
				}
				public void OnClick() {
					Role role = DWGameManager.getInstance().m_role;
					if(role.m_gold>=m_rb_ceng*Integer.valueOf(soul.m_priceByMoney)){
						if( soul.m_state == Soul.状态_正在激活 ){
							DWGameManager.getInstance().getSendMessage().sendPromoteSoulLevel((byte)soul.m_page,soul.m_index,(byte)0,m_rb_ceng);
						}
						DWControlsManager.getInstance().removeControl(soul_frame);
					}else{
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B, "您的金钱不足！");
					}
				}
			});

			soul_frame.addControl(button1);
			soul_frame.addControl(button2);
		
		}
		DWControlsManager.getInstance().addControl(soul_frame);
	}
	
	public void init() {
		// TODO Auto-generated method stub
		
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			for(int i=0;i<m_qihunxy.length;i++){
				for(int j=0;j<m_qihunxy[i].length;j++){
					
					m_qihunxy[i][j][0]=m_qihunxy[i][j][0]-5;
					
				}
			}
		}
		
		m_img_close=UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		m_tgBitmapTab01 = UIWindows.createImage("/img/newui/biaoqianye_1.gnp");//标签页选项
		m_tgBitmapTab02 = UIWindows.createImage("/img/newui/biaoqianye_2.gnp");//标签页选项
		m_img_background=UIWindows.createImage("/img/newui/beibaobj_1.gnp");
		
		img_button=UIWindows.createImage("/img/newui/anniu_6.gnp");
		img_anxia=UIWindows.createImage("/img/newui/anniu_6ax.gnp");
		
		m_img_icon = new Bitmap[2];
		m_img_icon[0] = UIWindows.createImage("/img/newui/xuewei_1.gnp");
		m_img_icon[1] = UIWindows.createImage("/img/newui/xuewei_2.gnp");
		
		m_img_name = new Bitmap[m_soulstr.length];
		m_img_name[0] = UIWindows.createImage("/img/newui/lianhunqh_1.gnp");
		m_img_name[1] = UIWindows.createImage("/img/newui/lianhunlh_1.gnp");
		m_img_name[2] = UIWindows.createImage("/img/newui/lianhunth_1.gnp");
		m_img_name[3] = UIWindows.createImage("/img/newui/lianhundh_1.gnp");
		m_img_name[4] = UIWindows.createImage("/img/newui/lianhunllh_1.gnp");
		m_img_name[5] = UIWindows.createImage("/img/newui/lianhunjh_1.gnp");
		
		m_cr_true = UIWindows.createImage("/img/newui/xuanzhonganniu_1.gnp");////单选被选中的图片
		m_cr_false = UIWindows.createImage("/img/newui/xuanzhonganniu_2.gnp");//单选没被选的图片
		
		img_img_title = UIWindows.createImage("/img/newui/lianhun_1.gnp");
		
		m_img_bk = UIWindows.createImage("/img/newui/lianhundt_1.gnp");
		
		m_Lbutton = UIWindows.createImage("/img/newui/haoyoufyan_2.gnp");
		m_Rbutton = UIWindows.createImage("/img/newui/haoyoufyan_1.gnp");
		
		 m_fen = 740;
		 m_fens = 368;
		 m_fenw = m_img_bk.getWidth()/m_fen;
		 m_fenh = m_img_bk.getHeight()/m_fens;
			
		 Tools.debugPrintln(m_img_bk.getWidth()+"份W:"+m_fenw);
		 Tools.debugPrintln(m_img_bk.getHeight()+"份H:"+m_fenh);
		 initSoulList();
		 initFrame();
	}

	public void open(byte index) {
		
	}

	public void close(byte index) {
		// TODO Auto-generated method stub
		if(tabControl!=null){
			tabControl.setSelectPage(0);
		}
		DWControlsManager.getInstance().removeControl(m_soul_frame);
		UIWindows.getInstance().m_zhanli.open_other(DWGameManager.getInstance().m_role.m_zhanli);//战力相关
	}

	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	public void logic() {
		// TODO Auto-generated method stub
		
	}
	
	public DWTabControl getTabControl() {
		if(tabControl!=null){
			Tools.debugPrintln("进入tabControl");
			return tabControl;
		}else{
			return null;
		}
	}
	
	public DWGrid getSoul(int page,int index) {
		if(grid!=null){
			Tools.debugPrintln("进入grid" +page+"["+ index+"]");
		
			return 	grid[page][index];
		}else{
			return null;
		}
	}
	
	public DWImageBox getShut() {
		if(closeImgBox!=null){
			Tools.debugPrintln("进入closeImgBox");
			return closeImgBox;
		}else{
			return null;
		}
	}
	
	private void touchfalse(boolean bool){
		Tools.debugPrintln("进入："+bool);
		if(bool){
			tabControl.setTouch(true);
			closeImgBox.setTouch(true);
			for(int i=0;i<grid.length;i++){
				grid[m_index][i].setTouch(true);
			}
		}else{
			tabControl.setTouch(false);
			closeImgBox.setTouch(false);
			for(int i=0;i<grid.length;i++){
				grid[m_index][i].setTouch(false);
			}
		}
	}
	
	public static final byte SOUL_SIZE = 6;
	public static final byte INDEX_MAXHP = 0;
	public static final byte INDEX_ATT = 1;
	public static final byte INDEX_DEFPHY = 2;
	public static final byte INDEX_DEFMAG = 3;
	public static final byte INDEX_HIT = 4;
	public static final byte INDEX_MISS = 5;
	
	//返回取值类型
	public String getValueType(int page){
		String temp = "";
		switch(page){
		case INDEX_ATT:
		case INDEX_DEFPHY:
		case INDEX_DEFMAG:
		    temp = "%";
			break;
		}
		return temp;
	}
	
	//魂列表缓存
	public List<CopyOnWriteArrayList<Soul>> m_soulList;
	public String m_curValue = "0";//当前魂页的值
	
	/** 初始化魂列表 */
	public void initSoulList(){
		if(m_soulList == null){
			m_soulList = new CopyOnWriteArrayList<CopyOnWriteArrayList<Soul>>();
			m_jieduan = new int[SOUL_SIZE];
			for(int i=0;i<SOUL_SIZE;i++){
				CopyOnWriteArrayList<Soul> list = new CopyOnWriteArrayList<Soul>();
				int size = m_qihunxy[i].length;
			    for(int j=0;j<size;j++){
			    	Soul soul = new Soul();
			    	soul.m_page = i;
			    	soul.m_index = j;
			    	soul.m_name = m_soulstr_name[i][j];
			    	soul.setPostion(m_qihunxy[i][j][0], m_qihunxy[i][j][1]);
			    	list.add(soul);
			    }
			    m_soulList.add(list); 
			}
		}
	}
	
    /**
     * 获取对应索引位的魂列表
     * @param index
     * @return
     */
	public CopyOnWriteArrayList<Soul> getSoulListByIndex(int index){
		CopyOnWriteArrayList<Soul> list = null;
		if(index >= INDEX_MAXHP && index <= INDEX_MISS){
			if(m_soulList != null){				
				list = m_soulList.get(index);
			}
		}
		return list;
	}
    
	/***
	 * 切换角色时用于还原数据
	 */
	public void release(){
		if(m_soulList != null){				
			for(CopyOnWriteArrayList<Soul> list:m_soulList){
				if(list != null){
					for(Soul soul:list){
						soul.reset();
					}
				}
			}
		}
		if(m_jieduan!=null){
			for(int i=0;i<m_jieduan.length;i++){
				m_jieduan[i]=0;
				if(m_tbstr[i]!=null)
				for(int j=0;j<m_tbstr[i].length;j++){
					m_tbstr[i][j].setText(m_soulstr_name[i][j]);
				}
			}
			m_curValue = "0";
		}
		if(tabControl!=null){
			tabControl.setSelectPage(0);
		}
	}
	
	/***
	 * 获取当前魂页已激活的数量
	 * @param page
	 * @return
	 */
	public int getCurSoulLevelByIndex(int page){
		int num = 0;
		CopyOnWriteArrayList<Soul> list = getSoulListByIndex(page);
		if(list != null){
			for(Soul soul:list){
				 if(soul.m_state == Soul.状态_已激活 || soul.m_state == Soul.状态_正在升级){
					 num++;
				 }
			}
		}
		return num;
	}
	
	
	public void refreshList(CopyOnWriteArrayList<Soul> list,int page){
		if(list != null){
			tabPage[page].setShow(true);

			String temp = null;
			for(Soul soul:list){
				if(soul != null){
					if(soul.m_state == Soul.状态_正在激活){
						temp = soul.m_name+soul.m_num+"层";
					}else{
						temp = soul.m_name;
					}
					m_tbstr[soul.m_page][soul.m_index].setText(temp);
				}
			}
		}
	}
	/***
	 * 根据索引页请求，打开魂页
	 * @param dis
	 * @throws IOException
	 */
	public void recvOpenSoulListByIndex(DataInputStream dis) throws IOException
	{
		try {
			Tools.debugPrintln("接收:"+GameProtocol.SC_OPEN_SOUL_BY_INDEX);
			DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_炼魂);
			byte state = dis.readByte();//打开结果
			if(state != 1){//失败跳出
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "打开失败！");
				return;
			}
			byte page = dis.readByte();//魂页
			m_index = page;
			Short size = dis.readShort();//当前已点完个数
			CopyOnWriteArrayList<Soul> list = getSoulListByIndex(page);
			String temp = null;
			//校验当前魂页已有缓存并且已点个数合法
			if(list != null && size >= 0){
				Soul soul = null;
				int curValue = dis.readInt();//读出当前显示的属性增加值
				m_curValue = String.valueOf(curValue)+getValueType(page);
				if (size > 0) {//如果有已经激活的，处理激活
					for (int i = 0; i < size; i++) {
						soul = list.get(i);
						soul.m_state = Soul.状态_已激活;// 状态
					}
				}
				soul = list.get(size);
				curValue = dis.readInt();//当前点完后的属性增加值	
				soul.m_state = Soul.状态_正在激活;
				soul.m_value = String.valueOf(curValue)+getValueType(page);							
				soul.m_num = dis.readShort();//当前已经点的次数
				soul.m_max = dis.readShort();//当前需要点的最大次数
				if(soul.m_num>=soul.m_max){
					soul.m_state = Soul.状态_已激活;// 状态
					m_curValue = soul.m_value;
				}
				temp = soul.m_name+soul.m_num+"层";
			}
			m_jieduan[page] = getCurSoulLevelByIndex(page);
			if(!DWControlsManager.getInstance().contains(m_soul_frame)){
				DWControlsManager.getInstance().addControl(m_soul_frame);
			}
			m_index = page;
			Tools.debugPrintln("打开魂页："+m_index);
			Tools.debugPrintln("已点完的个数："+size);
			if(tabControl!=null){
				tabControl.setSelectPage(m_index);
			}
			Tools.debugPrintln(m_strps[page]+":"+m_curValue);
			m_tbname[page].setText("<mark c=FF0000>"+m_strps[page]+":"+m_curValue+"</mark>");
			refreshList(list, page);
			touchfalse(true);	
		} catch (Exception e) {
			Tools.debug(e);
		}
	}
	
	/***
	 * 接收某一个魂殿的详细数据
	 * 
	 * @param dis
	 * @throws IOException
	 */
	public void recvSoulByIndex(DataInputStream dis) throws IOException {
		DWGameManager.getInstance().getSendMessage()
				.unlockNetwork(GameMessage.锁_炼魂);
		try {
			Tools.debugPrintln("接收：" + GameProtocol.SC_SEE_SOUL_BY_INDEX);
			byte state = dis.readByte();// 结果
			if (state != 1) {// 失败跳出
				return;
			}
			byte page = dis.readByte();// 魂页
			short index = dis.readShort();// 魂级
			index -= 1;// 转回索引
			CopyOnWriteArrayList<Soul> list = getSoulListByIndex(page);
			Soul seeSoul = null;
			boolean flag = false;
			if (list != null) {
				int size = list.size();
				// 验证合法
				if (index < size) {
					int curValue = dis.readInt();
					Soul soul = list.get(index);
					seeSoul = soul;
					int num = soul.m_num;
					String moneyStr = soul.m_priceByMoney;
					soul.m_num = dis.readShort();
					soul.m_max = dis.readShort();
					Tools.debugPrintln("soul.m_num = "+soul.m_num+";soul.m_max="+soul.m_max);
					long money = dis.readLong();
					long spirit = dis.readLong();
					soul.m_priceByMoney = String.valueOf(money);
					soul.m_priceBySpirit = String.valueOf(spirit);
					soul.m_value = String.valueOf(curValue)
							+ getValueType(page);
					
				
					
					// 判断播放升级动画
					if ( moneyStr != null && soul.m_num > num) {
						soul.m_ActorIndex = 1;
						// 正好升到下一级
						if (soul.m_num == soul.m_max) {
							soul.m_state = Soul.状态_正在升级;
							flag = true;
							m_curValue = soul.m_value;
							m_tbname[page].setText("<mark c=FF0000>"+m_strps[page]+":"+m_curValue+"</mark>");
							// 如果还有下一级
							if (index + 1 < size) {
								soul = list.get(index + 1);
								soul.m_state = Soul.状态_正在激活;
							}
						}
					}
					m_jieduan[page] = getCurSoulLevelByIndex(page);
					refreshList(list, page);
				}

				if (seeSoul != null && !flag) {
					m_rb_ceng=1;
					m_rb_index=0;
					touchfalse(true);
					drawmenu(seeSoul);
				}

			}
		} catch (Exception e) {
			Tools.debug(e);
		}
	}
	
	/***
	 * 点击升级是否成功
	 * @param dis
	 * @throws IOException
	 */
	public void recvPromoteSoulResult(DataInputStream dis) throws IOException
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_炼魂);
		try {
			byte state = dis.readByte();//结果
			String temp = "";
			switch(state){
			case 0:
				temp = "失败";
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "升级失败！");
				break;
			case 1:
				temp = "成功";
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "升级成功！");
				break;
			case 2:
				temp = "出错失败";
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "升级出错！");
				break;
			case 3:
				temp = "验证失效";
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "验证失败！");
				break;
			case 4:
				temp = "金币不足";
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "升级失败！您的金钱不足！");
				break;
			case 5:
				temp = "精魂不足";
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "升级失败！您的精魂不足！");
				break;
			case 6:
				temp = "等级已满";
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "升级失败！层级已满！");
				break;
			case 7:
				temp = "跃级";
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "升级失败！跃级错误！");
				break;
			}
		} catch (Exception e) {
			Tools.debug(e);
		}
	}
	//请求打开魂页
	public void testAnnouncement() {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		;
		try {
			dos.writeByte(1);
			dos.writeByte((byte)m_index);
			dos.writeShort(2);
			dos.writeInt(100);
			dos.writeInt(100);
			dos.writeShort(1);
			dos.writeShort(10);

		} catch (IOException e) {
		}
		ByteArrayInputStream das = new ByteArrayInputStream(baos.toByteArray());
		DataInputStream dis = new DataInputStream(das);

		try {
			recvOpenSoulListByIndex(dis);
		} catch (Exception e) {

		}
	}


}
